import json  # 用于解析用户输入的字典字符串

user = {"admin": 123456}
c_user = {"lisi": 123456}
data_lines =[
    {"ID": "1", "类别": "吃饭支出", "账户": "交通银行", "金额": "247.0", "时间": "2016-03-02", "说明": "买汉堡等"},
    {"ID": "2", "类别": "工资收入", "账户": "现金", "金额": "12345.0", "时间": "2016-03-15", "说明": "3月工资"},
    {"ID": "3", "类别": "吃饭支出", "账户": "现金", "金额": "19.8", "时间": "2016-04-02", "说明": "买早餐"},
    {"ID": "4", "类别": "转账支出", "账户": "现金", "金额": "175.0", "时间": "2016-06-18", "说明": "朋友借款"},
    {"ID": "5", "类别": "吃饭支出", "账户": "工商银行", "金额": "8000.0", "时间": "2016-08-28", "说明": "朋友聚餐"},
    {"ID": "6", "类别": "工资收入", "账户": "工商银行", "金额": "5000.0", "时间": "2016-10-28", "说明": "10月工资"},
    {"ID": "7", "类别": "工资收入", "账户": "交通银行", "金额": "5000.0", "时间": "2016-10-28", "说明": "兼职收入"},
    {"ID": "8", "类别": "礼金支出", "账户": "现金", "金额": "500.0", "时间": "2016-10-29", "说明": "朋友结婚"},
    {"ID": "9", "类别": "其他支出", "账户": "现金", "金额": "1500.0", "时间": "2016-10-29", "说明": "买鞋"},
    {"ID": "10", "类别": "交通支出", "账户": "交通银行", "金额": "2300.0", "时间": "2016-10-29", "说明": "国庆节旅游"},
    {"ID": "11", "类别": "吃饭支出", "账户": "工商银行", "金额": "1000.0", "时间": "2016-10-29", "说明": "买衣服"},
    {"ID": "12", "类别": "工资收入", "账户": "现金", "金额": "1000.0", "时间": "2016-10-30", "说明": "外快"},
    {"ID": "13", "类别": "交通支出", "账户": "现金", "金额": "2000.0", "时间": "2016-10-30", "说明": "机票"},
    {"ID": "14", "类别": "工资收入", "账户": "现金", "金额": "5000.0", "时间": "2016-10-30", "说明": "兼职费"}
]

print("欢迎进入度小满家庭对账系统，请先登录")

may = input("请选择登录方式: 普通/管理员\n")
if may == "普通":
    account = input("请输入账号: ")
    password = input("请输入密码: ")
    if account in c_user and c_user[account] == int(password):
        print("登录成功")
        print("1.查询所有 2.按条件查询")
        while True:
            num = input("请选择功能（1-2）：")
            if num == "1":
                for item in data_lines:
                    print(
                        f"ID：{item['ID']}，类别：{item['类别']}，账户：{item['账户']}，金额：{item['金额']}，时间：{item['时间']}，说明：{item['说明']}")
                print("查询成功")
            if num == "2":
                condition = input("请输入要查询的条件：ID、类别、账户、金额、时间、说明\n")
                context = input("请输入条件类别：")
                for i in data_lines:
                    if i[condition] == context:
                        print(i)

if may == "管理员":
    for i in range(3):
        account = input("请输入账号: ")
        password = input("请输入密码: ")  # 改为字符串输入，兼容非数字密码
        # 验证密码时转换为int（原user中密码是数字），若密码含非数字需调整
        if account in user and user[account] == int(password):
            print("登录成功")
            print("1.添加账务 2.编辑账务 3.删除账务 4.查询账务 5.退出系统")
            while True:
                num = input("请选择功能（1-5）：")  # 明确提示输入

                # 1. 添加账务
                if num == "1":
                    try:
                        # 输入格式示例：{"ID":"15", "类别":"购物支出", "账户":"现金", "金额":"300", "时间":"2023-10-01", "说明":"买水果"}
                        new_data = input("请输入要添加的账务（格式：字典，包含ID、类别、账户、金额、时间、说明）：")
                        new_dict = json.loads(new_data)  # 将字符串转换为字典
                        # 验证必要键是否存在
                        required_keys = ["ID", "类别", "账户", "金额", "时间", "说明"]
                        if all(key in new_dict for key in required_keys):
                            data_lines.append(new_dict)
                            print("添加成功")
                        else:
                            print("格式错误！请包含所有必要字段（ID、类别、账户、金额、时间、说明）")
                    except json.JSONDecodeError:
                        print("格式错误！请输入正确的字典格式（如：{\"ID\":\"15\",...}）")
                    print("1.添加账务 2.编辑账务 3.删除账务 4.查询账务 5.退出系统")

                # 2. 编辑账务
                elif num == "2":
                    try:
                        id_input = input("请输入要编辑的账单ID（如1、2...）：")
                        # 查找ID对应的索引
                        index = None
                        for i, item in enumerate(data_lines):
                            if item["ID"] == id_input:
                                index = i
                                break
                        if index is None:
                            print("未找到该ID的账单")
                            continue
                        # 输入新内容并转换为字典
                        new_content = input("请输入新的账单内容（格式：字典，包含ID、类别、账户、金额、时间、说明）：")
                        new_dict = json.loads(new_content)
                        if all(key in new_dict for key in required_keys):
                            data_lines[index] = new_dict
                            print("编辑成功")
                        else:
                            print("格式错误！请包含所有必要字段")
                    except json.JSONDecodeError:
                        print("格式错误！请输入正确的字典格式")
                    print("1.添加账务 2.编辑账务 3.删除账务 4.查询账务 5.退出系统")

                # 3. 删除账务
                elif num == "3":
                    try:
                        id_input = input("请输入要删除的账单ID（如1、2...）：")
                        # 查找ID对应的索引
                        index = None
                        for i, item in enumerate(data_lines):
                            if item["ID"] == id_input:
                                index = i
                                break
                        if index is None:
                            print("未找到该ID的账单")
                            continue
                        del data_lines[index]  # 删除整个元素
                        print("删除成功")
                    except Exception as e:
                        print(f"删除失败：{e}")
                    print("1.添加账务 2.编辑账务 3.删除账务 4.查询账务 5.退出系统")

                # 4. 查询账务
                elif num == "4":
                    print("所有账务信息如下：")
                    for item in data_lines:
                        print(
                            f"ID：{item['ID']}，类别：{item['类别']}，账户：{item['账户']}，金额：{item['金额']}，时间：{item['时间']}，说明：{item['说明']}")
                    print("查询成功")
                    print("1.添加账务 2.编辑账务 3.删除账务 4.查询账务 5.退出系统")

                # 5. 退出系统
                elif num == "5":
                    print("退出成功")
                    break

                # 处理无效输入
                else:
                    print("无效输入！请输入1-5之间的数字")

        else:
            if i == 2:
                print("次数已用尽，退出系统")
                break
            print(f"账号或密码错误请重新输入，您还剩{2 - i}次尝试机会")